﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using System.Data;
using System.Data.SqlTypes;
using Telerik.Web.UI;

namespace Web.Loyalty
{
    public partial class NewLoyalty : System.Web.UI.Page
    {
        static string prevPage = String.Empty;
        static long? assignedToID = null;
        static USER user;
        static ROLE r;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            assignedToID = null;
            user = (USER)Session["User"];
            if (user != null)
            {
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                {
                    Response.Redirect("../Home.aspx");
                }
            }
            if (Request.UrlReferrer != null)
            {
                prevPage = Request.UrlReferrer.ToString();
                hdntxtPrevPage.Value = prevPage;
            }
            LoadAllUser();

        }
        private void LoadAllUser()
        {
            ddlAssignedTo.Items.Clear();
            List<USER> users = UserService.ListAll();
            if (users != null)
            {
                foreach (USER u in users)
                {
                    ROLE r2 = RoleService.GetRoleByID((long)u.ROLE_ID);
                    if (((bool)u.IS_ADMIN) || ((bool)r2.IS_MANAGER))
                    {
                        ddlAssignedTo.Items.Add(new RadComboBoxItem(u.SUR_NAME + " " + u.NAME, u.ID.ToString()));
                    }
                }
            }
        }
        private int addNewLoyalty()
        {
            LOYALTY lo = new LOYALTY();
            //General information
            DateTime dt = DateTime.Now;
            lo.DATE_CREATED = dt;
            lo.DATE_MODIFIED = dt;
            //Details
            lo.NAME = txtName.Text;
            lo.DESCRIPTION = txtDescription.Text;
            if ((bool)user.IS_ADMIN)
            {
                if (ddlAssignedTo.SelectedValue != "-1")
                {
                    lo.ASSIGN_TO_ID = Convert.ToInt64(ddlAssignedTo.SelectedValue);
                    lo.ASSIGN_TO_NAME = ddlAssignedTo.SelectedItem.Text;
                }
            }
            //else
            //{
            //    lo.ASSIGN_TO_ID = user.ID;
            //    lo.ASSIGN_TO_NAME = user.SUR_NAME + " " + user.NAME;
            //}
            lo.POINTS_TO_AWARD = rntxtPointToAward.Value;
            lo.EXCHANGE_RATE = rntxtExchangeRate.Value;
            return LoyaltyService.AddNewLoyalty(lo);
        }
        protected void btn_Save_Click(object sender, EventArgs e)
        {
            int newQuoteResult = addNewLoyalty();
            if (newQuoteResult == 1)
            {
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"ShowMessageAndRedirect('" + "Add new loyalty successful !" + "','" + "Loyalty.aspx" + "');");
                //MessageBox.Show(this.Page, Page.GetType(), "Add new loyalty successful !", "Loyalty.aspx");
            }
            else
                RadAjaxManager.GetCurrent(this).ResponseScripts.Add(@"showMessage('" + "Can not add new loyalty !" + "');");
                //ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not add new loyalty !');", true);
        }
    }
}